﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;

namespace _1042009_1042034
{
    public partial class uctDanhSachPhongThue : UserControl
    {
        public uctDanhSachPhongThue()
        {
            InitializeComponent();
        }

        int iColStt, iColMaPhong, iColTenPhong, iColDonGia, iColSoNgayThue, iColThanhTien, iColCheck;
        protected void CreateColumn()
        {
            this.dgv.Columns.Clear();

            
            this.dgv.AddTextColumn(true, "STT", "STT");
            List<PhongDTO> lst = Business.PhongService.LayDanhSachPhongDaThue();
            DataTable dt = Business.PhongService.ConvertListPhongDTOToDataTable(lst);

            this.dgv.AddComboBoxColumns(false, "MaPhong", "Mã phòng", dt, PhongDTO.ValueMember, PhongDTO.ValueMember);
            this.dgv.AddComboBoxColumns(false, "TenPhong", "Tên phòng", dt, PhongDTO.ValueMember, PhongDTO.DisplayMember);

            this.dgv.AddTextColumn(true, "DonGia", "Đơn Giá");
            this.dgv.AddTextColumn(true, "SoNgayThue", "Số ngày thuê");
            this.dgv.AddTextColumn(true, "ThanhTien", "Thành tiền");
            this.dgv.AddCheckBoxColumn("Check", "Check");

            iColStt = this.dgv.GetColumIndexByName("STT");
            iColMaPhong = this.dgv.GetColumIndexByName("MaPhong");
            iColTenPhong = this.dgv.GetColumIndexByName("TenPhong");
            iColDonGia = this.dgv.GetColumIndexByName("DonGia");
            iColSoNgayThue = this.dgv.GetColumIndexByName("SoNgayThue");
            iColThanhTien = this.dgv.GetColumIndexByName("ThanhTien");
            iColCheck = this.dgv.GetColumIndexByName("Check");
        }
        void SetupDataGriew()
        {

            this.dgv.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
            this.dgv.Location = new Point(0, 0);
            this.dgv.ScrollBars = ScrollBars.Both;
            this.dgv.TopLeftHeaderCell.Value = "Select All";
            this.dgv.BackgroundColor = Color.White;
            // dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader; 
            // cho hien dong trang.
            this.dgv.AllowUserToAddRows = true; 
        }
        private void uctDanhSachPhongThue_Load(object sender, EventArgs e)
        {
            this.CreateColumn();
            this.SetupDataGriew();
            
        }

        int iStt = 0;

        int TinhKhoangCanh2Ngay(DateTime d1, DateTime d2)
        {
            int dd = ( d1 - d2).Days + 1;
           // int kq = int.Parse(dd.ToString());
            return dd;
        }
        private void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int iRowIndex = this.dgv.SelectedCells[0].RowIndex;
                int iColIndex = this.dgv.SelectedCells[0].ColumnIndex;

                int iMa = int.Parse(this.dgv.SelectedCells[0].Value.ToString());

                PhieuThueDTO pt = Business.PhieuThueService.LayPhieuThueHienTaiCuaPhong(iMa);

                this.dgv[this.iColMaPhong, iRowIndex].Value = iMa.ToString();
                this.dgv[this.iColTenPhong, iRowIndex].Value = iMa.ToString();
                this.dgv[this.iColDonGia, iRowIndex].Value = pt.DonGia.ToString();

                int SoNgayThue = TinhKhoangCanh2Ngay(DateTime.Now, pt.NgayBatDauThue);
                this.dgv[this.iColSoNgayThue, iRowIndex].Value = SoNgayThue.ToString();

                //thanh tien
                double ThanhTien = pt.DonGia * SoNgayThue;
                this.dgv[this.iColThanhTien, iRowIndex].Value = ThanhTien.ToString();

                if (this.dgv[this.iStt, iRowIndex].Value == null)
                {
                    this.iStt++;
                    this.dgv[this.iStt, iRowIndex].Value = this.iStt.ToString();
                }
                if (iColIndex != this.iColCheck)
                {
                    this.dgv[this.iColCheck, iRowIndex].Value = "Checked";
                }
            }
            catch { }
        }

    }
}
